﻿using System.Web.Mvc;
using System.Web.Security;
using CodeStash.Filters;
using CodeStash.Models.Security;
using CodeStash.Services;




namespace CodeStash.Controllers
{
    public class HomeController : BaseTagCloudEnabledController
    {

        private readonly IMembershipService membershipService;
        private readonly ILoggerService loggerService;



        public HomeController(IMembershipService membershipService, 
            ILoggerService loggerService, ITagCloudService tagCloudService) : base(tagCloudService)
        {
            this.membershipService = membershipService;
            this.loggerService = loggerService;
        }



        //[Authorize]
        [RenderTagCloud]
        public ActionResult Index()
        {
            if (!this.ControllerContext.RouteData.DataTokens.ContainsKey("area"))
            {
                this.ControllerContext.RouteData.DataTokens.Add("area", "Home");
            }
            if (User.Identity.IsAuthenticated)
            {
                MembershipUser user = membershipService.GetUserByUserName(User.Identity.Name);
                UserSettingsProfileModel profile = UserSettingsProfileModel.GetUserProfile(User.Identity.Name);
                profile.IsOpenIdLoggedInUser = Session["EncryptedPasswordForUserToWriteDown"] != null;
                profile.Save();
                ViewBag.Message = Session["EncryptedPasswordForUserToWriteDown"];
                Session["IsOpenIdLoggedInUser"] = profile.IsOpenIdLoggedInUser;
                loggerService.Info(string.Format("Home : User '{0}' is Authenticated", profile.UserName));
            }
            else
            {
                loggerService.Info(string.Format("Home : User Identity '{0}' is NOT Currently Authenticated", User.Identity));
            }

            return View();
        }


        [RenderTagCloud]
        public ActionResult HowToUse()
        {
            return View();
        }
      
        
        [RenderTagCloud]
        public ActionResult Plugins()
        {
            return View();
        }
        
    }
}
